package com.eastfair.home.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
//import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.home.dao.ReportBuilderAuditMapper;
import com.eastfair.home.dto.ReportBuilderAuditDTO;
import com.eastfair.home.entity.ReportBuilderAudit;
import com.eastfair.home.exceptioncode.HomeExceptionCode;
import com.eastfair.home.service.ReportBuilderAuditService;
import com.eastfair.home.vo.ReportBuilderAuditVO;
import com.eastfair.venuebooking.api.ServiceBookingFeign;
import com.eastfair.venueservice.consts.CommonConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 业务实现类
 * 报馆搭建商信息审核记录
 * </p>
 *
 * @author dq
 * @date 2022-08-08
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class ReportBuilderAuditServiceImpl extends SuperServiceImpl<ReportBuilderAuditMapper, ReportBuilderAudit> implements ReportBuilderAuditService {

    @Resource
    private ServiceBookingFeign serviceBookingFeign;

    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<ReportBuilderAudit> modelList) {
        modelList.forEach(s -> s.setIsDeleted(BusinessConstant.YES));
        return R.successDef();
    }

    @Override
    protected R<Boolean> handlerSave(ReportBuilderAudit model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        return R.successDef();
    }

    @Override
    public ReportBuilderAuditVO getVOByReportSpaceAuditId(Long reportSpaceAuditId) {
        log.info("getReportBuilderAuditVOByReportSpaceAuditId - 报馆审核ID查询报馆搭建商审核, reportSpaceAuditId={}", reportSpaceAuditId);
        ReportBuilderAudit reportBuilderAudit = getByReportSpaceAuditId(reportSpaceAuditId);
        if (reportBuilderAudit == null) {
            return null;
        }
        return BeanUtil.toBean(reportBuilderAudit, ReportBuilderAuditVO.class);
    }

    @Override
    public ReportBuilderAudit getByReportSpaceAuditId(Long reportSpaceAuditId) {
        ReportBuilderAuditDTO query = new ReportBuilderAuditDTO();
        query.setReportSpaceAuditId(reportSpaceAuditId);
        return getReportBuilderAudit(query);
    }

    @Override
    public ReportBuilderAudit getByReportSpaceId(Long reportSpaceId) {
        ReportBuilderAuditDTO query = new ReportBuilderAuditDTO();
        query.setReportSpaceId(reportSpaceId);
        return getReportBuilderAudit(query);
    }

    @Override
    public ReportBuilderAudit getReportBuilderAudit(ReportBuilderAuditDTO query) {
        log.info("getReportSiteAudit - 获取报馆搭建商审核, query={}", query);
        QueryWrapper<ReportBuilderAudit> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .lambda()
                .last("limit 1")
                .eq(ReportBuilderAudit::getIsDeleted, BusinessConstant.DELETE_NO)
                .eq(query.getReportSpaceAuditId() != null, ReportBuilderAudit::getReportSpaceAuditId, query.getReportSpaceAuditId())
                .eq(query.getReportSpaceId() != null, ReportBuilderAudit::getReportSpaceId, query.getReportSpaceId())
        ;
        return getOne(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean submitReportBuilderAudit(ReportBuilderAuditDTO reportBuilderAuditDTO) {
        removeByReportSpaceId(reportBuilderAuditDTO.getReportSpaceId());
        return saveReportBuilderAudit(reportBuilderAuditDTO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveReportBuilderAudit(ReportBuilderAuditDTO reportBuilderAuditDTO) {
        ReportBuilderAudit reportBuilderAudit = BeanUtil.toBean(reportBuilderAuditDTO, ReportBuilderAudit.class);
        return save(reportBuilderAudit);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateByReportSpaceAuditId(ReportBuilderAuditDTO reportBuilderAuditDTO, Long reportSpaceAuditId) {
        reportBuilderAuditDTO.setReportSpaceAuditId(reportSpaceAuditId);
        ReportBuilderAudit reportBuilderAudit = getByReportSpaceAuditId(reportBuilderAuditDTO.getReportSpaceAuditId());
        if (reportBuilderAudit == null) {
            throw BizException.wrap(HomeExceptionCode.REPORT_SITE_AUDIT_NOT_EXIST);
        }
        CopyOptions copyOptions = CopyOptions
                .create()
                .setIgnoreProperties(CommonConstants.COMMON_IGNORE_PROPERTIES);
        BeanUtil.copyProperties(reportBuilderAuditDTO, reportBuilderAudit, copyOptions);
        return updateAllById(reportBuilderAudit);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByReportSpaceId(Long reportSpaceId) {
        ReportBuilderAudit reportBuilderAudit = getByReportSpaceId(reportSpaceId);
        if (reportBuilderAudit == null) {
            return false;
        }
        List<ReportBuilderAudit> list = new ArrayList<>();
        list.add(reportBuilderAudit);
        return removeByIdsOfLogic(list);
    }
}
